#!/Perl/bin/perl -w
# package controller;
use lib 'd:/test_project/lib';

use Dumps;
use Data::Dumper;
use Controllers::errorController;

use strict;
use CGI ();
CGI->compile(':all');
use CGI::Carp;
use Controllers::indexController;
# use Cwd qw(abs_path);
# use File::Basename qw(dirname);
 
use Template;
use Template::Context;
use Template::Stash;
use Error qw(:try);

our $cgi;
our $language;
my $conf;
my ($controller,$action,$route);

use DBI;
#use DBD::mysql;
# our $dbh;

BEGIN {
	$conf = {
		style=>"main",
		out_path=>"d:/test_project/lib/",
		work_path=>"d:/test_project/lib/",
		#project_dir = abs_path($0),
		#enter_dir => dirname(abs_path($0)),
	};
	# html();
	# use Cwd qw();
	# my $path = Cwd::cwd();
	
	#print "$path\n";
	#print $conf->{enter_dir};
	
	$route = $ENV{SCRIPT_NAME};
	(undef,$controller,$action,$route) = split('/',$route);	
	$controller = undef if length($controller) == 0;
	$action = undef if length($action) == 0;
	if ((!defined($action)) && (defined($controller))){
		if (-e $conf->{work_path}."Controllers/" . $controller . "Controller.pm") {
			$action = 'index';#'default'
		}
		else {
			$action = $controller;
			$controller = 'index';#'default'
		}
	} elsif ((!defined($action)) && (!defined($controller))) {
		$action = 'index';
		$controller = 'index';	
	}
	eval('use Controllers::' . $controller . 'Controller;');	
}
$cgi = CGI->new; 


sub bye {
       # exit;
}

&main;

sub main {	
	# if(!defined($controller)) {
		# if(defined($ENV{CONTENT_LENGTH}) && $ENV{CONTENT_LENGTH}==0) {
			# html();
			# return 1;
		# }

		# load_page("unknown.cmd.htm", {
		    # title=>'Can`t load page',
		    # message=>'Error! Browser hasn`t any possible command!'
		# });
		# bye();
	# } else {
		load_action();
	# }
	bye();
}

sub html {
#      print "Content-type: text/html\n\n";   
        print "Content-type: text/html ";
        print "Cache-Control: no-cache, must-revalidate, private, no-store, s-maxage=0, max-age=0, post-check=0, pre-check=0";
        print "Pragma: no-cache";
        print "Expires: Fri, 2 Jan 2011 00:00:00 GMT";
        print "\n\n";
}

# sub load_page {
	# html();
	# my $template = Template->new(INCLUDE_PATH => 'd:/test_project/lib/Views/Templates');
	# if (@_ == 2) {
		# my ($file, $vars) = @_;
		# if (!$template->process($file, $vars)) {
			# print "<br/><br/><b>Fatal error:</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			# my $template_error = $template->error();
			# print $template_error;
		# }
	# }
	# else {
		# return 0;
	# }
	# 1;
# }

sub load_action {
	html();
#	my $dbh = DBI->connect('dbi:ODBC:driver={SQL Server};Server=localhost\SQLEXPRESS;database=testproject;uid=bohdan;pwd=test;',undef,undef,{ RaiseError => 0, AutoCommit => 0 })
	my $dbh = DBI->connect('dbi:mysql:test:localhost:3306', 'test', '')
		or print "Content-type: text/html\r\n\r\n connect failed: $DBI::errstr";
	print "1";
	
	# my $template = Template->new(
					# INCLUDE_PATH => 'd:/test_project/lib/Views/Templates', 
					# #OUTPUT_PATH => 'd:/test_project/htdocs', 
					# #COMPILE_DIR => 'd:/test_project/htdocs',					
					# ENCODING => 'utf8',
					# EVAL_PERL => 1
				# );
	# my $tmp = defined($conf->{style}) ? $conf->{style}.".template.htm" : "main.template.htm";
	
	# # #print '<img src="top.png"/>';
	# # #print $conf->{enter_dir};
	# # #print dirname(abs_path($0));
	
	# # #print Dumper $template;
	
	# my $vars;
	# if ($vars = defined(&{'Controllers::' . $controller . 'Controller::' . $action . 'Action'}) ? eval('Controllers::' . $controller . 'Controller::' . $action . 'Action()') : eval('Controllers::errorController::indexAction("Can`t fint controller or action")')) {
		# html();
		# if (!$template->process($tmp, $vars)) {
			# print "<br/><br/><b>Fatal error:</b><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			# print $template->error();
		# }
	# } else {
		# load_page("unknown.cmd.htm", { 
		    # title=>'Unknown page!',
		    # message=>'Error 404! Server can`t load this page!'
		# });
		# return 0;
	# }
	# 1; 
}
 
1;